package com.example.dell.newstart.type;

import android.content.Context;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.newstart.R;
import com.example.dell.newstart.base.RequestInterface;
import com.example.dell.newstart.bean.Product;


import java.util.List;

/**
 * created by Dongkai on 2018/12/12
 * 根据搜索得到的产品列表 的 产品的适配器
 */

public class ProductAdapter extends RecyclerView.Adapter {
    private List<Product> productList;//接收构造方法中传过来的产品的集合
    private Context context;//上下文

    //构造方法
    public ProductAdapter(Context context, List<Product> productList) {
        this.productList = productList;
        this.context = context;
    }

    @NonNull
    //视图容器（创建看得见的东西）
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_product, null);
        return new ViewHolder(view);
    }

    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        ViewHolder viewHolder1 = (ViewHolder) viewHolder;
        //获取第 i 个产品
        final Product product = productList.get(i);
        //绑定数据
        Glide.with(context)
                .load(RequestInterface.baseImgPath + product.getMainImage())
                .into(viewHolder1.productImageView);//绑定图片
        viewHolder1.name.setText(product.getName());//商品名称
        viewHolder1.subTitle.setText(product.getSubtitle());//商品副标题
        String productString = "￥" + product.getPrice();//拼装用于显示的价格（加上了￥）
        viewHolder1.price.setText(productString);//价格
        //点击列表中的商品，跳转到商品详情，并把商品信息传递过去
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              /*  Intent intent=new Intent(context, ProductDetailActivity.class);
                //把商品信息从intent传递到 ProductDetailActivity 活动
                Bundle bundle=new Bundle();
                bundle.putSerializable("product",product);
                intent.putExtras(bundle);
                context.startActivity(intent);*/
            }
        });
    }

    //显示多少条数据，一般数据集合有多少就显示多少
    @Override
    public int getItemCount() {
        return productList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView productImageView;
        private TextView name, subTitle, price;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            productImageView = itemView.findViewById(R.id.iv_product);
            name = itemView.findViewById(R.id.tv_name);
            subTitle = itemView.findViewById(R.id.tv_sub_title);
            price = itemView.findViewById(R.id.tv_price);
        }
    }

    public void addAll(List<Product> productList) {
        this.productList.addAll(productList);
        notifyDataSetChanged();
    }
}
